package cc.rengu.redp.flowable.bizmodel.calculate;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

@Slf4j
public class SpecifyFields extends FieldAlgorithmBase {
    SpecifyFields(String params) {
        super(params);
    }

    @Override
    public String cal(JSONObject jsonObj) {
        if (params.contains("+")){
            String[] fieldGroup = params.split("\\+");
            StringBuilder rs = new StringBuilder();
            for (String field : fieldGroup) {
                String rsStr = jsonObj.getString(field.trim());
                if (StringUtils.isBlank(rsStr)) {
                    log.error("无法在数据源中解析字段[{}]", field);
                    return "";
                }
                rs.append(rsStr.trim());
            }

            return rs.toString();
        } else {
            String rsStr = jsonObj.getString(params.trim());
            if (StringUtils.isBlank(rsStr)) {
                log.error("无法在数据源中解析字段[{}]", params);
                return "";
            }
            return rsStr.trim();
        }
    }
}
